package com.hongye.house.controller;

import com.alibaba.fastjson.JSON;
import com.hongye.common.utils.PageUtils;
import com.hongye.common.utils.R;
import com.hongye.house.entity.SaleHomeEntity;
import com.hongye.house.service.SaleHomeService;
import com.hongye.house.vo.SaleHomeVo;
import com.hongye.house.vo.SaleQueryVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;
import java.util.List;
import java.util.Map;



/**
 * 房源：售房表
 *
 * @author lins
 * @email 695565697@qq.com
 * @date 2021-10-14 13:32:23
 */
@RestController
@RequestMapping("house/salehome")
public class SaleHomeController {
    @Autowired
    private SaleHomeService saleHomeService;



    /*
    房源es搜索
     */
    @RequestMapping("eshousesearch")
    public List<SaleHomeVo> eshousesearch(@RequestBody SaleQueryVo vo) throws Exception {
        List<SaleHomeVo> saleHomeVos=saleHomeService.eshousesearch(vo);
        return saleHomeVos;
    }

    /*
    房源传入es
     */
/*    @Scheduled(fixedRate = 2000)*/
    public void eshouseadd(){
        saleHomeService.eshouseadd();
    }


    /*
    房源列表
     */
    @RequestMapping("salelist")
    private R salelist(){
        List<SaleHomeVo> saleHomeVos=saleHomeService.getsalelist();
        return R.ok().put("data",saleHomeVos);
    }

    /*
  es搜索
   */
    @RequestMapping("/esoldsearch")
    public List<SaleHomeEntity> esoldsearch(@RequestBody SaleQueryVo vo ) throws Exception {
        List<SaleHomeEntity> saleHomeEntities=saleHomeService.esoldsearch(vo);
        return saleHomeEntities;
    }

    /*
    es搜索
     */
    @RequestMapping("/esnewsearch")
    public List<SaleHomeEntity> esnewsearch(@RequestBody SaleQueryVo vo) throws Exception {
        List<SaleHomeEntity> saleHomeEntities=saleHomeService.esnewsearch(vo);
        return saleHomeEntities;
    }

    /*
    首先把二手房子的信息传入es
     */
    //@Scheduled(fixedRate = 2000)
    public void esoldinsert(){
        saleHomeService.esoldinsert();
    }

    /*
    首先把新房子的信息传入es
     */
    //@Scheduled(fixedRate = 2000)
    public void esnewinsert(){
        saleHomeService.esnewinsert();
    }

    /*
    经纪人小程序端-首页数据提供接口
    二手房数首页数据提供接口
 */
    @RequestMapping("/secondhandhousing")
    public R secondhandhousing(){
        Integer  saleHomeEntities= saleHomeService.secondhandhousing();
        return R.ok().put("data",saleHomeEntities);
    }



    /*
    经纪人小程序端-首页数据提供接口
    新房数首页数据提供接口
     */
    @RequestMapping("/numberofnewhouses")
    public R numberofnewhouses(){
        Integer saleHomeEntities= saleHomeService.numberofnewhouses();
        return R.ok().put("data",saleHomeEntities);
    }




    /*
    售房列表
     */
    @RequestMapping("/salelist/{houseagent}")
    public List<SaleHomeEntity> salelist(@PathVariable("houseagent")Integer houseagent){
        List<SaleHomeEntity> saleHomeEntities= saleHomeService.salelist(houseagent);
        return saleHomeEntities;
    }






    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = saleHomeService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{houseId}")
    public R info(@PathVariable("houseId") Integer houseId){
		SaleHomeEntity saleHome = saleHomeService.getById(houseId);

        return R.ok().put("saleHome", saleHome);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody SaleHomeEntity saleHome){
		saleHomeService.save(saleHome);

        return R.ok().put("data", JSON.toJSONString(saleHome));
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody SaleHomeEntity saleHome){
		saleHomeService.updateById(saleHome);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Integer[] houseIds){
		saleHomeService.removeByIds(Arrays.asList(houseIds));

        return R.ok();
    }

}
